using System;
using System.Collections.Generic;
using System.Text;
using DCI6.Client.API;
using dds.tns;
using ddsLib;

namespace DCIAPIRMSampleCode {
    internal class AdvertisementSampleCode {
        public Advertisement CreateAdvertisement() {
            Console.WriteLine("Creating a new Advertisement Object...");
            /**
        * Please note, that below code will demonstrate how to set ALL possible fields
        * which are exposed in the dds.xsd. Not all of the fields are required to be set 
        * in order to create advertisement. Please adapt this code based on your application's
        * requirements.
        */
            Advertisement advertisement = new Advertisement();

            /**
       *  Corresponds to areaOfOccurance in dds.xsd
       */
            advertisement.SetBounds(-90, -180, 90, 180);

            //Advertise ddsAdvertisement = new Advertise();


            /**
       * Please note that creatorID is an essentially a unique key for an
       * advertisement that user can create. This means, that only 1
       * advertisement can be created with a particular value of creatorID.
       * Users will not be able to create 2 or more advertisements with the
       * same creator id. If users do not set this field that DDS will
       * populate the value automatically. Therefore, it is safer not to set
       * this fields directly in the code.
       * 
       * Corresponds to CreatorID in dds.xsd
       */
            advertisement.CreatorID = "creator1";

            PolicyType policy = new PolicyType();

            /**
       * Creating a rule for DISTRIBUTION. Corresponds to policy/rule field in
       * dd.xsd
       * 
       * The appropriate values for this field as per dds.xsd are: YES NO
       */
            Rule distributionRule = new Rule();
            distributionRule.Name = dds.tns.Enumerations.PolicyRuleType.DISTRIBUTION;
            distributionRule.Value = "YES";

            /**
       * Creating a rule for SLOW_CLIENT. Corresponds to policy/rule field in
       * dd.xsd
       * 
       * The appropriate values for this field as per dds.xsd are: DROP_ALL
       *  DROP_EARLIEST
       *       
       * */

            Rule capacityRule = new Rule();
            capacityRule.Name = dds.tns.Enumerations.PolicyRuleType.CAPACITY_EXCEEDED;
            capacityRule.Value = "DROP_EARLIEST";

            /**
       * Adding capacity and distribution rules to the policy 
       */
            advertisement.IAdvertiseType.Policy.Rule.Add(distributionRule);
            advertisement.IAdvertiseType.Policy.Rule.Add(capacityRule);

            ControlPolicyType controlPolicy = new ControlPolicyType();

            /**
      * Creating accessibleTo values. 
      * Corresponds to: policy/controlPolicy/accessibleTo in the dds.xsd
      */
            //dds.tns.AccessibleTo accessibleTo = new dds.tns.AccessibleTo();
            //accessibleTo.Role.Add("DDSSubscriber");
            //controlPolicy.AccessibleTo = accessibleTo;


            /**
       * Creating publishableTo values. 
       * Corresponds to: policy/controlPolicy/publishableTo in the dds.xsd
       */
            PublishableTo publishableTo = new PublishableTo();
            publishableTo.Role.Add("FBCB2");
            controlPolicy.PublishableTo = publishableTo;

            /**
       * Creating archivableTo values. 
       * Corresponds to: policy/controlPolicy/archivableTo in the dds.xsd
       * The appropriate values for this field as per dds.xsd are: true false
       */
            controlPolicy.ArchiveableTo = true;

            /**
       * Creating priority values. 
       * Corresponds to: policy/controlPolicy/priority in the dds.xsd
       * The appropriate values for this field as per dds.xsd are: true false
       */
            controlPolicy.Priority = true;
            /**
       * Creating query only values. 
       * Corresponds to: policy/controlPolicy/queryOnly in the dds.xsd
       * The appropriate values for this field as per dds.xsd are: true false
       */
            controlPolicy.QueryOnly = false;
            /**
       * Creating compression type values. 
       * Corresponds to: policy/controlPolicy/compression in the dds.xsd
       * The appropriate values for this field as per dds.xsd are: NONE
       * GZIP
       * FI
       * GFI
       */
            controlPolicy.Compression = dds.tns.Enumerations.CompressionType.NONE;

            /**
       * Setting control policy to the advertisement's policy.
       */
            policy.ControlPolicy = controlPolicy;

            /**
       * Setting policy to an Advertisement
       */
            advertisement.IAdvertiseType.Policy = policy;

            /**
       * Creating title.
       * Corresponds to: metadata/title in the dds.xsd
       */

            dds.ddms.Title title = new dds.ddms.Title();
            title.ClassifiedBy = "DOD";
            advertisement.IAdvertiseType.Metadata.Title = title;

            advertisement.SetTitle("POS-RPT:MCS:ARMY1");


            /**
       * Creating description
       * Corresponds to: metadata/description in the dds.xsd
       */
            advertisement.SetDescription("PASS:");

            /**
       * Creating source
       * Corresponds to: metadata/source in the dds.xsd
       */
            dds.ddms.Source source = new dds.ddms.Source();
            source.Qualifier = "qualifer";
            source.SchemaHref = "schema";

            /**
       * Creating Organization
       * Corresponds to metadata/creator/organization in the dds.xsd
       */
            dds.ddms.Creator organizationCreator = new dds.ddms.Creator();
            dds.ddms.Organization organization = new dds.ddms.Organization();
            organization.Name.Add("organization");
            dds.ddms.OrganizationType_Type_Seq orgType = new dds.ddms.OrganizationType_Type_Seq();
            orgType.Phone.Add("phone");
            orgType.Email.Add("email");
            organization.OrganizationType_Type_Seq = orgType;
            organizationCreator.Organization = organization;

            /**
       * Creating Person
       * Corresponds to metadata/creator/person in the dds.xsd
       */
            dds.ddms.Creator personCreator = new dds.ddms.Creator();
            dds.ddms.Person person = new dds.ddms.Person();
            person.Name.Add("name");
            dds.ddms.PersonType_Type_Seq personType = new dds.ddms.PersonType_Type_Seq();
            personType.Surname = "surname";
            personType.UserID = "userID";
            personType.Affiliation = "affiliation";
            personType.Phone.Add("phone");
            personType.Email.Add("email");
            person.PersonType_Type_Seq = personType;
            personCreator.Person = person;

            /**
       * Creating Service
       * Corresponds to metadata/creator/service in the dds.xsd
       */
            dds.ddms.Creator serviceCreator = new dds.ddms.Creator();
            dds.ddms.Service service = new dds.ddms.Service();

            service.Name.Add("name");
            dds.ddms.ServiceType_Type_Seq serviceType = new dds.ddms.ServiceType_Type_Seq();
            serviceType.Phone.Add("phone");
            serviceType.Email.Add("email");
            service.ServiceType_Type_Seq = serviceType;
            serviceCreator.Service = service;

            /**
       * Adding organization, person, service to the list of creators.
       */

            advertisement.IAdvertiseType.Metadata.Creator.Add(organizationCreator);
            advertisement.IAdvertiseType.Metadata.Creator.Add(personCreator);
            advertisement.IAdvertiseType.Metadata.Creator.Add(serviceCreator);
            /**
       * Creating categories
       * Corresponds to metadata/subjectCoverage/category in the dds.xsd
       */
            List<String> categories = new List<String>();
            categories.Add("POS-RPT");
            advertisement.Categories = categories;

            /**
       * Creating keywords
       * Corresponds to metadata/subjectCoverage/keyword in the dds.xsd
       */
            List<String> keywords = new List<String>();
            keywords.Add("MCS");
            keywords.Add("FBCB2");
            advertisement.Keywords = keywords;

            /**
       * Creating start time
       * Corresponds to metadata/temporalCoverage/TimePeriod/start in the dds.xsd
       */
            advertisement.StartDTG = DateTime.Now;

            /**
       * Creating end time
       * Corresponds to metadata/temporalCoverage/TimePeriod/end in the dds.xsd
       */
            advertisement.SetEndDTG(DateTime.Now + TimeSpan.FromHours(5)); //5 hrs from now

            /**
       * Creating classification for an advertisement
       * Corresponds to: metadata/security/classification in the dds.xsd
       */
            advertisement.ClassificationType = Advertisement.Classification.UNCLASSIFIED;

            return advertisement;
        }

        public void Advertise(Advertisement advertisement, String ddsConfigConnectionID) {
            /**
       * Calling Create2() on the Advertisement Object will send the 
       * Advertise Request to the DDS Configured with the connectionID
       * whose value is equal to the value stored in DDSConfigConnectionID.
       * If the DCI Service does not currently have the DDSConnectionID
       * registered an Exception will be thrown. If the call is successfull 
       * the Advertisement will be created on DDS.
       */
            Console.WriteLine("Sending Advertise Request..");
            advertisement.Create2(ddsConfigConnectionID);
            String advertisementID = advertisement.AdvertisementId;
            Console.WriteLine("Advertisement [{0}] Created", advertisementID);
        }

        public void UnAdvertise(Advertisement advertisement, String ddsConfigConnectionID) {
            /**
       * Calling Unadvertise on the Advertisment Object will send the 
       * UnAdvertise Request to the DDS Configured with the connectionID
       * whose value is equal to the value stored in DDSConfigConnectionID.
       * If the DCI Service does not currently have the DDSConnectionID
       * registered an Exception will be thrown. If call is successfull the
       * Advertisement will be removed from DDS. An Advertisement must be 
       * created on DDS by calling the create or create2 method before calling unadvertise.
       */
            String id = advertisement.AdvertisementId;
            Console.WriteLine("Sending UnAdvertise Request...");
            advertisement.Unadvertise(ddsConfigConnectionID);
            Console.WriteLine("Advertisement [{0}] UnAdvertised", id);
        }
    }
}